<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>列车交路列表</title>
    <script type="text/javascript">
      var newTrainRouterWin;
      var editTrainRouterWin;
      var trainRouterDS;

      Ext.onReady(function(){
        //检修任务管理---------------------------------------------------------
        //生成有关检修任务的Grid

        trainRouterDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize
          },
          proxy: new Ext.data.HttpProxy({
            url: '/trainRouter/getTrainRouters?trainId='+ <%=params[:trainId]%>
          }),
          reader:new Ext.data.JsonReader({
            totalProperty: 'total',
            root:'root'
          },[
            {name:'id'},
            {name:'line',mapping: 'router.line'},
            {name:'sequence'},
            {name:'train_number',mapping: 'router.train_number'},
            {name:'length',mapping: 'router.length'},
          ])
        });
        var trainRouterSM = new Ext.grid.CheckboxSelectionModel();
        var trainRouterCM = new Ext.grid.ColumnModel([
          new Ext.grid.RowNumberer({width: 30}),
          trainRouterSM,
          {header:'交路循环顺序',sortable:true,dataIndex:'sequence',width:80},
          {header:'交路组合',sortable:true,dataIndex:'train_number'},
          {header:'运行交路',sortable:true,dataIndex:'line'},
          {header:'一次走行公里',sortable:true,dataIndex:'length'}
        ]);

        function maintenanceRender(value, cellmeta, record, rowIndex, columnIndex, store){
          var trainRouterId = store.getAt(rowIndex).get('id');
          var trainId = store.getAt(rowIndex).get('train_id');
          var taskBeginTime = store.getAt(rowIndex).get('begin_time');
          var isEnd = store.getAt(rowIndex).get('isEnd');
          var sn = store.getAt(rowIndex).get('sn').toString();
          var day = 2                                                           //因为不能传送时间，因此在此对时间进行转换
          var hour = taskBeginTime.slice(11,13)
          var minute = taskBeginTime.slice(14,16)
          var taskBeginMinute = day*1440 + hour*60 + minute*1
          while (taskBeginMinute> 1920)
          {
            taskBeginMinute = taskBeginMinute-1440
          }
          return "<a style=cursor:pointer;color:blue onclick=readMaintenanceWindow("
            +trainRouterId+","+trainId+","+taskBeginMinute+","+isEnd+",'"+sn+"')>进入</a>"
        }

        function chartRender(value, cellmeta, record, rowIndex, columnIndex, store){
          var trainRouterId = store.getAt(rowIndex).get('id');
          var trainId = store.getAt(rowIndex).get('train_id');
          var taskBeginTime = store.getAt(rowIndex).get('begin_time');
          sn = store.getAt(rowIndex).get('sn').toString();
          var day = 2                                                           //因为不能传送时间，因此在此对时间进行转换
          var hour = taskBeginTime.slice(11,13)
          var minute = taskBeginTime.slice(14,16)
          var taskBeginMinute = day*1440 + hour*60 + minute*1
          while (taskBeginMinute> 1920)
          {
            taskBeginMinute = taskBeginMinute-1440
          }
          return "<a style=cursor:pointer;color:green onclick=readChartWindow("
            +trainRouterId+","+trainId+","+taskBeginMinute+",'"+sn+"')>进入</a>"
        }

        var trainRouterGrid =  new Ext.grid.GridPanel({
          ds: trainRouterDS,
          cm: trainRouterCM,
          sm: trainRouterSM,
          width: screenWidth-240,
          height: screenHeight-310,
          iconCls: 'icon-grid',
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: trainRouterDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          }),
          tbar: ['-',{
              text: '添加',
              width:50,
              iconCls: 'addItem',
              handler : function(){
                newTrainRouterWin = new Ext.Window({
                  layout: 'fit',
                  width:797,
                  height:548,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  title: '添加检修任务',
                  autoLoad: {
                    url: "/trainRouter/newTrainRouter",
                    scripts: true
                  }
                });
                newTrainRouterWin.on("destroy",function(){
                  trainRouterDS.reload();
                });
                newTrainRouterWin.resizable = false;
                newTrainRouterWin.show();
              }
            },{
              text: '编辑',
              width:50,
              iconCls: 'editItem',
              handler : function(){
                var records = trainRouterGrid.getSelectionModel().getSelections();
                if(records.length==0){
                  Ext.MessageBox.alert('提示信息', '请先选中一条记录后，再编辑。');
                }else{
                  var trainRouterId = records[0].get('id');
                  editTrainRouterWin = new Ext.Window({
                    title: '编辑检修任务',
                    layout:'fit',
                    width:797,
                    height:548,                                                  
                    closeAction:'destroy',
                    constrain:true,
                    plain: true,
                    modal: true,
                    autoLoad: {
                      url: "/trainRouter/editTrainRouter",
                      params: {
                        trainRouterId: trainRouterId
                      },
                      scripts: true
                    }
                  });
                  editTrainRouterWin.on("destroy",function(){
                    trainRouterDS.reload();
                  });
                  editTrainRouterWin.resizable = false;
                  editTrainRouterWin.show();
                }
              }
            },
            {
              text:'删除',
              width:50,
              iconCls: 'remove',
              handler:function(){
                if(trainRouterGrid.getSelectionModel().hasSelection()){
                  var rs=trainRouterGrid.getSelectionModel().getSelections();
                  var ids=[];
                  var trainRouterSn='';
                  for(var i=0;i<rs.length;i++){
                    var data=rs[i].data;
                    ids.push(data.id);
                    trainRouterSn+=data.sn+'<br>';
                  }
                  var keys=Ext.util.JSON.encode(ids);
                  Ext.MessageBox.confirm('警告','确定删除以下检修任务？<br><font color="red">' + trainRouterSn + '</font>',function (button){
                    if(button=='yes'){
                      Ext.Ajax.request({
                        url:'/trainRouter/delete',
                        method:'post',
                        success:function(response,opts){
                          var data = Ext.util.JSON.decode(response.responseText);
                          if(data.success&&data.info=='success') {
                            trainRouterDS.reload();
                            Ext.Msg.alert('提示信息', '已成功删除检修任务。');
                          } else {
                            Ext.Msg.alert('提示信息', data.info);
                          }
                        },
                        params: {"id":keys}
                      });
                    }
                  });
                }else{
                  Ext.Msg.alert('提示信息','请选择要删除的检修任务');
                }
              }
            },'-'
          ]
        });
        trainRouterGrid.render('trainRouterList');
        trainRouterDS.load();
      })
    </script>
  </head>
  <body>
    <div id="trainRouterList"></div>
  </body>
</html>
